package ndk.caro.gameplays;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import ndk.caro.views.myCaroView;

public class myPiece {
	public static final int iXPLAYER = 1;
	public static final int iOPLAYER = 0;
	public static final int NONE = -1;

	private int iValue;
	private int iCol;
	private int iRow;
	private FourAngle mFourAngle;

	private Point pLocation;
	private Rect rect;

	public FourAngle getFourAngle() {
		return mFourAngle;
	}
	public void setFourAngle(FourAngle mFourAngle) {
		this.mFourAngle = mFourAngle;
	}
	public void setFourAngle() { 
		if (this.iCol >= myCaroGame.iGAME_CENTER_ARRAY_PIECE
				&& this.iRow >= myCaroGame.iGAME_CENTER_ARRAY_PIECE)
			this.setFourAngle(FourAngle.ONE);
		else if (this.iCol >= myCaroGame.iGAME_CENTER_ARRAY_PIECE
				&& this.iRow < myCaroGame.iGAME_CENTER_ARRAY_PIECE)
			this.setFourAngle(FourAngle.TWO);
		else if (this.iCol < myCaroGame.iGAME_CENTER_ARRAY_PIECE
				&& this.iRow < myCaroGame.iGAME_CENTER_ARRAY_PIECE)
			this.setFourAngle(FourAngle.THREE);//
		else
			this.setFourAngle(FourAngle.FOUR);
		
		//Log.i(myDetails.tag, "(" + this.iCol + "," + this.iRow + ") = " + this.mFourAngle.toString());
	}
	private boolean isInRectLimitDisplay(PointF p) {
		if (p.x > myCaroView.RECT_LIMIT_DRAWING.left - myCaroView.iEDGE_PIECE
				&& p.x < myCaroView.RECT_LIMIT_DRAWING.right
				&& p.y > myCaroView.RECT_LIMIT_DRAWING.top - myCaroView.iEDGE_PIECE
				&& p.y < myCaroView.RECT_LIMIT_DRAWING.bottom)
			return true;
		return false;
	}
	public PointF getPointRealOfPiece(PointF pNewCenter, int iEDGE_PIECE) {
		PointF p = null;			
			switch (this.mFourAngle) {
			case ONE:
				p = new PointF(pNewCenter.x
						+ (this.iCol - myCaroGame.iGAME_CENTER_ARRAY_PIECE)
						* iEDGE_PIECE, pNewCenter.y
						+ (this.iRow - myCaroGame.iGAME_CENTER_ARRAY_PIECE)
						* iEDGE_PIECE);
				
			//	Log.i(myDetails.tag, "ONE");
				
				break;
			case TWO:
				p = new PointF(pNewCenter.x
						+ (this.iCol - myCaroGame.iGAME_CENTER_ARRAY_PIECE)
						* iEDGE_PIECE, pNewCenter.y
						- (myCaroGame.iGAME_CENTER_ARRAY_PIECE - this.iRow)
						* iEDGE_PIECE);
				//Log.i(myDetails.tag, "TWO");
				break;
			case THREE:
				p = new PointF(pNewCenter.x
						- (myCaroGame.iGAME_CENTER_ARRAY_PIECE - this.iCol)
						* iEDGE_PIECE, pNewCenter.y
						- (myCaroGame.iGAME_CENTER_ARRAY_PIECE - this.iRow)
						* iEDGE_PIECE);
			//	Log.i(myDetails.tag, "THREE");
				break;
			case FOUR:
				p = new PointF(pNewCenter.x
						- (myCaroGame.iGAME_CENTER_ARRAY_PIECE - this.iCol)
						* iEDGE_PIECE, pNewCenter.y
						+ (this.iRow - myCaroGame.iGAME_CENTER_ARRAY_PIECE)
						* iEDGE_PIECE);
				//Log.i(myDetails.tag, "FOUR");
				break;
			
			}		
		
		return p; 		
	}
	public PointF getPointOnDisplay(PointF pNewCenter, int iEDGE_PIECE) {
		PointF p = this.getPointRealOfPiece(pNewCenter,iEDGE_PIECE);		
		if(this.isInRectLimitDisplay(p)) return p; 
		return null;		 
	}
	public void setCol(int iCol) {
		this.iCol = iCol;
		this.pLocation.x = iCol;
	}
	public int getCol() {
		return iCol;
	}
	public int getRow() {
		return iRow;
	}
	public void setRow(int iRow) {
		this.iRow = iRow;
		this.pLocation.y = iRow;
	}
	public int getValue() {
		return iValue;
	}
	public void setValue(int iValue) {
		this.iValue = iValue;
	}
	public Point getLocation() {
		return pLocation;
	}
	public void setLocation(Point pLocation) {
		this.setLocation(pLocation.x, pLocation.y);
	}
	public void setLocation(int iCol, int iRow) {
		this.pLocation = new Point(iCol, iRow);
		this.setCol(iCol);
		this.setRow(iRow);
	}
	public void setRect(int x, int y) {
		this.rect.set(x * myCaroView.iEDGE_PIECE,
				y * myCaroView.iEDGE_PIECE, x
						* myCaroView.iEDGE_PIECE + myCaroView.iEDGE_PIECE,
				y * myCaroView.iEDGE_PIECE + myCaroView.iEDGE_PIECE);
	}
	public Rect getRect() {
		return rect;
	}
	public myPiece(int iCol, int iRow) {
		this.pLocation = new Point(iCol, iRow);
		this.iCol = iCol;
		this.iRow = iRow;
		this.iValue = NONE;
		//Log.i(myDetails.tag, "(" + this.iCol + "," + this.iRow + ") = ");
	}
	public enum FourAngle {
		ONE, TWO, THREE, FOUR
	}
}
